package delayed.boundedbuffer;

import java.util.Vector;

public class BoundedBuffer<E> {

	private Vector<E> store = new Vector<E>();
	private int MAX;

	public BoundedBuffer() {
		setMax(-1);
	}

	public BoundedBuffer(int max) {
		setMax(max); // set to -1 for infinite maximal buffer size
	}

	private void setMax(int max) {
		MAX = max;
	}

	public synchronized void put(E item) throws InterruptedException {
		while (store.size() == MAX) {
			wait();
		}
		store.add(item);
		notifyAll();
	}

	public synchronized E get() throws InterruptedException {
		while (store.size() == 0) {
			wait();
		}
		E item = store.elementAt(0);
		store.removeElementAt(0);
		notifyAll();
		return item;
	}
}
